<template>
	<xn-form-container
		:title="type ? '批量转让线索' : '转让线索'"
		:width="600"
		:visible="visible"
		:destroy-on-close="true"
		:body-style="{ 'padding-top': '0px' }"
		@close="onClose"
	>
		<div v-if="!type">
			<div></div>
		</div>
		<div v-else></div>
		<a-form ref="formRef" :model="formData" :rules="formRules" class="p-10">
			<a-form-item label="线索名称">
				<ellipsis :length="55" tooltip>
					{{ list.map((item) => item.customerName).join('、') }}
				</ellipsis>
			</a-form-item>
			<template v-if="!type">
				<a-form-item label="呼叫状态">
					<span>{{ list[0]?.callStatus }}</span>
				</a-form-item>
				<a-form-item label="跟进状态">
					<span>{{ followUpMap[list[0]?.followUpStatus] }}</span>
				</a-form-item>
			</template>
			<template v-else>
				<a-form-item label="线索数量">
					<span>{{ list.length }}</span>
				</a-form-item>
			</template>
			<a-form-item label="转让对象" name="personId">
				<a-tree-select
					v-model:value="formData.personId"
					:tree-data="store.selectableDeptList"
					allow-clear
					show-search
					tree-node-filter-prop="label"
					:fieldNames="fieldNames"
					max-tag-count="responsive"
					:dropdownMatchSelectWidth="false"
					treeNodeFilterProp="name"
					placeholder="请选择"
					@change="onPersonChange"
				/>
			</a-form-item>
		</a-form>
		<template #footer>
			<div class="flex items-center justify-end">
				<a-space>
					<a-button type="primary" :loading="submitLoading" @click="onSubmit">确定</a-button>
					<a-button style="margin-right: 8px" @click="onClose">取消</a-button>
				</a-space>
			</div>
		</template>
	</xn-form-container>
</template>

<script setup>
	import { required } from '@/utils/formRules'
	import { globalStore } from '@/store'
	import customerApi from '@/api/biz/customerApi'
	import { followUpMap } from '@/views/biz/contractManagement/enum/contractEnum'

	const store = globalStore()

	const submitLoading = ref(false)

	// 默认是关闭状态
	const visible = ref(false)
	const formRef = ref()
	const emit = defineEmits({ successful: null })

	const fieldNames = {
		label: 'name',
		value: 'id'
	}

	// 表单数据
	const formData = ref({})

	const type = ref(0)
	const list = ref([])

	// 打开抽屉
	const onOpen = (value) => {
		visible.value = true
		type.value = value.type
		list.value = value.list
	}
	// 关闭抽屉
	const onClose = () => {
		visible.value = false
	}

	const onPersonChange = (value, node, extra) => {
		formData.value.personName = value ? node[0] : null
	}

	// 默认要校验的
	const formRules = {
		personId: [required('请选择转让对象')]
	}
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value.validate().then(() => {
			submitLoading.value = true
			const { personId, personName } = formData.value
			customerApi
				.transferClue({
					personId: personId.substring(2),
					personName,
					poolIdParamList: list.value.map((item) => ({ id: item.id }))
				})
				.then(() => {
					onClose()
					formRef.value.resetFields()
					formData.value = {}
					emit('successful')
				})
				.finally(() => {
					submitLoading.value = false
				})
		})
	}

	// 调用这个函数将子组件的一些数据和方法暴露出去
	defineExpose({
		onOpen
	})
</script>

<style scoped type="less"></style>
